package com.example.textview.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.textview.R;

import org.w3c.dom.Text;

public class CustomDialog extends Dialog implements View.OnClickListener,TimePicker.OnTimeChangedListener  {

    private TextView mtvTitle,mtvMessage,mtvCancel,mtvConfim;
   public TimePicker timePicker;
    private String title,msg,cancel,confim;
    private IOnCancelListener cancelListener;
    private IOnConfimListener confimListener;
    private IOnTimeChangedListener timeChangedListener;

    public void setTimeChangedListener(IOnTimeChangedListener timeChangedListener) {
        this.timeChangedListener = timeChangedListener;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setCancel(String cancel,IOnCancelListener listener) {
        this.cancel = cancel;
        this.cancelListener=listener;
    }

    public void setConfim(String confim,IOnConfimListener listener) {
        this.confim = confim;
        this.confimListener=listener;
    }



    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom_dialog);



        //设置宽度
        WindowManager m=getWindow().getWindowManager();
        Display d=m.getDefaultDisplay();
        WindowManager.LayoutParams p=getWindow().getAttributes();
        Point size=new Point();
        d.getSize(size);
        p.width= (int) (size.x*0.9);
        getWindow().setAttributes(p);


        mtvTitle=findViewById(R.id.tv_title);
        mtvMessage=findViewById(R.id.tv_message);
        mtvCancel=findViewById(R.id.tv_cancel);
        mtvConfim=findViewById(R.id.tv_confim);

        timePicker=findViewById(R.id.time_picker);

        timePicker.setIs24HourView(true);
        timePicker.setOnTimeChangedListener(this);

        if(!TextUtils.isEmpty(title))
          mtvTitle.setText(title);

        if(!TextUtils.isEmpty(msg)) {
            mtvMessage.setText(msg);
        }
        if(!TextUtils.isEmpty(cancel)) {
            mtvCancel.setText(cancel);
        }
        if(!TextUtils.isEmpty(confim)) {
            mtvConfim.setText(confim);
        }

        mtvConfim.setOnClickListener(this);
        mtvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.tv_cancel:
                if(cancelListener!=null)
                {
                    cancelListener.onCancel(this);

                }
                dismiss();
                break;
            case R.id.tv_confim:

                if(confimListener!=null)
                {
                    confimListener.onCancel(this);
                }
                dismiss();
                break;
        }
    }

    @Override
    public void onTimeChanged(TimePicker timePicker, int i, int i1) {
            if( this.timeChangedListener!=null)
            {
              //onTimeChanged(timePicker,i,i1);
                this.timeChangedListener.onTimeChange(timePicker,i,i1);
            }
    }

    public interface IOnCancelListener{
        void onCancel(CustomDialog customDialog);
    }

    public interface IOnConfimListener{
        void onCancel(CustomDialog customDialog);
    }

    public interface IOnTimeChangedListener{
        void onTimeChange(TimePicker timePicker, int hour, int secend);
    }
}
